package com.tg.app.activity.device.settings;

import android.os.Bundle;
import androidx.annotation.Nullable;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.base.BaseActivity;
import com.tange.core.backend.service.http.ClientObserver;
import com.tg.network.socket.http.TGHttp;
import com.tg.data.http.entity.ShareDeviceBean;
import com.tg.appcommon.android.AccountValidatorUtil;
import com.appbase.custom.constant.CommonConstants;
import com.tg.app.R;


import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class ShareActivity extends BaseActivity {

    private EditText editText;
    private Button confirm;
    long device_id;
    private ImageButton clear;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        setContentView(R.layout.activity_share);
        hideActionBar();
        device_id = getIntent().getLongExtra(CommonConstants.EXT_DEVICE_ID,-1);
        initView();
        modifyToolBar();
    }

    @Override
    public boolean shouldSetStatusBarColor() {
        return true;
    }

    protected void initView(){
        RelativeLayout toolbar = findViewById(R.id.toolbar);
        TextView tv = toolbar.findViewById(R.id.device_name);
        tv.setText(getString(R.string.share_to));
        ImageButton back = toolbar.findViewById(R.id.back_toolbar);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
              public void onClick(View v) {
                ShareActivity.this.finish();
            }
        });
        editText = findViewById(R.id.share_phone);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String str = s.toString().trim();
                if(TextUtils.isEmpty(str)){
                    clear.setVisibility(View.INVISIBLE);
                }else{
                    clear.setVisibility(View.VISIBLE);
                }
                if (AccountValidatorUtil.isEmail(str)||AccountValidatorUtil.isMobile(str)){
                    confirm.setEnabled(true);
                }else {
                    confirm.setEnabled(false);
                }
            }
        });
        InputFilter inputFilter = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if(source.equals(" "))return "";
                else return null;
            }
        };
        editText.setFilters(new InputFilter[]{inputFilter});
        confirm = findViewById(R.id.share);
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View v) {
                String account = editText.getText().toString().trim();
                if(AccountValidatorUtil.isMobile(account)||AccountValidatorUtil.isEmail(account)){
                    shareDevice(account);
                }else{
                    Toast.makeText(ShareActivity.this,R.string.tips_account_format_error,Toast.LENGTH_SHORT).show();
                }
            }
        });
        clear = findViewById(R.id.share_clear_edit);
        clear.setOnClickListener(new View.OnClickListener() {
            @Override
              public void onClick(View v) {
                editText.setText("");
            }
        });
    }

    private void shareDevice(String account){
        HashMap<String, String> map = new HashMap<>();
        map.put("account",account);
        map.put("device_id", String.valueOf(device_id));
        TGHttp.getInstance().shareDevice(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ClientObserver<ShareDeviceBean>() {

                    @Override
                    protected void onSuccess(ShareDeviceBean content) {
                        Toast.makeText(getApplicationContext(),getString(R.string.share_success),Toast.LENGTH_SHORT).show();
                        ShareActivity.this.finish();
                    }

                    @Override
                    protected void onResponseError(int errorCode, String errorInfo) {
                        Toast.makeText(getApplicationContext(),errorInfo,Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    protected void onOtherError(String error) {
                        Toast.makeText(getApplicationContext(),getString(R.string.share_failed),Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    protected void onFinish() {

                    }
                });
    }
}

